<?php

class Familymember_Widget_ProfileFamilyController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    try {
      // CHECK IF LOGGED IN
      $viewer = Engine_Api::_()->user()->getViewer();
      if ( !$viewer->getIdentity() ) {
        $this->setNoRender();
      }
      
      // CHECK IF PROPER USER PROFILE 
      $subject = Engine_Api::_()->core()->getSubject();
      if ( !($subject instanceof User_Model_User) && !$subject->getIdentity() ) {
        $this->setNoRender();
      }
      
      // CHECK IF FRIEND
      if ( !$viewer->isSelf($subject) && !$viewer->membership()->isMember($subject) ) {
        $this->setNoRender();
      }
      
      $rows = Engine_Api::_()->getDbTable('member', 'familymember')->fetchAll("user_id = ".$subject->getIdentity()." AND approved = 1");
      $familymembers = array();
      foreach ( $rows as $familymember ) {
        $familymember = Engine_Api::_()->user()->getUser($familymember->resource_id);
        if ( $familymember->getIdentity() && ($familymember instanceof User_Model_User) ) {
          $familymembers[] = $familymember;
        }
      }
      $this->view->familymembers = $familymembers;
    }
    catch ( Exception $e ) {
      echo $e->getMessage();
    }
  }
}